package com.liu.frame.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.liu.frame.R;
import com.liu.frame.config.Constant;

import cn.finalteam.okhttpfinal.HttpRequest;


/**
 * 加载弹窗工具
 *
 * @author Updated by Cao.yh on 2015-9-30
 */
public class LoadingUtil {

    public static LoadingUtil mLoadingUtil;
    private AnimationDrawable animationDrawable;//加载的动画

    private LoadingUtil() {
    }

    public static LoadingUtil getInstance() {
        return mLoadingUtil == null ? mLoadingUtil = new LoadingUtil() : mLoadingUtil;
    }

    /**
     * 自定义dialog
     **/
    private Dialog mDialog;

    /**
     * @param context
     * @param message       提示消息
     * @param cancelBoolean 是否可以取消
     * @param tag           请求tag
     */
    public void showDialog(Context context, String message, boolean cancelBoolean, String tag) {
        dismissDialog();// 如果dialog存在就关闭以前的dialog
        if (context != null) {
            mDialog = createLoadingDialog(context, message, cancelBoolean, tag);
            mDialog.show();
            animationDrawable.start();
        }
    }

    /**
     * 可以点击返回取消
     *
     * @param context
     * @param tag
     */
    public void showDialog(Context context, String tag) {
        showDialog(context, Constant.TOAST_LOADING, true, tag);
    }

    /**
     * 可以点击返回取消
     *
     * @param context
     * @param tag
     */
    public void showDialog(Context context, String tag, boolean isCancel) {
        showDialog(context, Constant.TOAST_LOADING, isCancel, tag);
    }

    /**
     * @param context
     * @param msg
     * @param cancelBoolean
     * @return
     * @description: 创建一个dialog
     * @author: baibai
     * @date: 2015-5-12 上午10:20:21
     */
    private Dialog createLoadingDialog(Context context, String msg, final boolean cancelBoolean, final String tag) {

        View v = View.inflate(context, R.layout.progressdialog_no_deal, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
        // main.xml中的ImageView
        ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);
        /**
         * 修改加载的动画 2016 09.29
         */
        spaceshipImage.setBackgroundResource(R.drawable.loading_anim);
        animationDrawable = (AnimationDrawable) spaceshipImage.getBackground();

//        TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
//        // 加载动画
//        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context, R.anim.progressdialog_anim);
//        hyperspaceJumpAnimation.setDuration(800);
//        // 使用ImageView显示动画
//        spaceshipImage.startAnimation(hyperspaceJumpAnimation);
        // spaceshipImage.setImageResource(R.anim.bga_refresh_loding);
//        tipTextView.setText(msg);// 设置加载信息
        Dialog loadingDialog = new Dialog(context, R.style.loading_dialog) {
            @Override
            public void onBackPressed() {
                if (!TextUtils.isEmpty(tag) && cancelBoolean) {
//                    HttpQueue.getInstance().cancelRequests(tag);
                    HttpRequest.cancel(tag);//取消网络请求
                }
                super.onBackPressed();
            }
        };// 创建自定义样式dialog
        loadingDialog.setCancelable(cancelBoolean);// 可以用“返回键”取消
        loadingDialog.setCanceledOnTouchOutside(false);//点击屏幕其他地方不能取消dialog
        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        return loadingDialog;

    }

    /**
     * 关闭对话框
     */
    public void dismissDialog() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
            animationDrawable.stop();

        }
    }

}
